<?php

/*

	WebPhrame - A rudimentary PHP web site framework
	Copyright (c) 2008 Joshua Bodine

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this program.  If not, see
	<http://www.gnu.org/licenses/>.

*/

class DBMySQL {
	private $conn, $config;
	public $status, $prefix;

	function __construct($host, $db, $user, $pass, $prf) {
		// Connect to the database
		$this->conn = mysql_connect($host, $user, $pass);
		if(! $this->conn)
			return;

		// Try to select the database to make sure it exists
		if(! mysql_select_db($db, $this->conn))
			return;

		// Store the variables
		$this->config['host'] = $host;
		$this->config['db'] = $db;
		$this->config['user'] = $user;
		$this->config['pass'] = $pass;
		$this->prefix = $prf;

		// Ugly hack since PHP5 doesn't let us abort in constructors
		$this->status = TRUE;
	}

	/*
	Note that because calls are passed to this object via
	call_user_func() in the containing object, arguments
	are passed as an array rather than as a list of arguments.
	As such, arguments have to be accessed through the array rather than
	by name. This should be fixed if there's a way to do so.
	*/

	// Internal function to ensure the MySQL connection is up.
	protected function dbReady() {
		if(!mysql_ping($this->conn)) {
			$this->conn = mysql_connect($this->config['host'],
					$this->config['user'],
					$this->config['pass']);
			if(! $this->conn)
				return;

			// Try to select the database again
			 mysql_select_db($this->config['db'], $this->conn);
		}
	}

	// Query expected to return zero values or one value
	public function query($args) {
		$this->dbReady();

		$result = mysql_query($args[0]);
		if(! $result || @mysql_num_rows($result) < 1)
			return;

		return mysql_result($result, 0);
	}

	// Query expected to return multiple column values for a single row
	public function queryRow($args) {
		$this->dbReady();

		$result = mysql_query($args[0]);
		if(! $result || @mysql_num_rows($result) < 1)
			return;

		return mysql_fetch_assoc($result);
	}

	// Query expected to return multiple row values for a single column
	public function queryCol($args) {
		$this->dbReady();

		$result = mysql_query($args[0]);
		if(! $result || @mysql_num_rows($result) < 1)
			return;

		$result_array = array();
		for($i = 0; $i < mysql_num_rows($result); ++$i)
			array_push($result_array, mysql_result($result, $i));

		return $result_array;
	}

	// Query expected to return muliple rows of values
	public function queryMulti($args) {
		$this->dbReady();

		$result = mysql_query($args[0]);
		if(! $result || @mysql_num_rows($result) < 1)
			return;

		// Compile and return a multidimensional array
		$result_array = array();
		while($entry = mysql_fetch_assoc($result))
			array_push($result_array, $entry);

		return $result_array;
	}
}

?>
